﻿namespace com.vs.General.BL
{
    using com.vs.General;
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class LicenseProcessing
    {
        private string constructLicenseKey(string stMachineName, int iNumUsers, DateTime creationDate, DateTime dtExpirationDate, string stExpirationAction, string stModules, string stFile, string stDir, string stBiosSerialNum, LicenseKeyConstants.LicenseKeyType licenseType)
        {
            object obj2 = (((((((((((((LicenseKeyConstants.stLICENSEKEYBIOSSERIALNUM + LicenseKeyConstants.stCOLON + stBiosSerialNum) + LicenseKeyConstants.stDODELIM) + LicenseKeyConstants.stLICENSEKEYMACHINENAME + LicenseKeyConstants.stCOLON + stMachineName) + LicenseKeyConstants.stDODELIM) + LicenseKeyConstants.stLICENSEKEYNUMBEROFUSERS + LicenseKeyConstants.stCOLON + iNumUsers.ToString()) + LicenseKeyConstants.stDODELIM) + LicenseKeyConstants.stLICENSEKEYEXPIRATIONDATE + LicenseKeyConstants.stCOLON + DateManager.ToStringWithInvariantCulture(dtExpirationDate)) + LicenseKeyConstants.stDODELIM) + LicenseKeyConstants.stLICENSEKEYEXPIRATIONACTION + LicenseKeyConstants.stCOLON + stExpirationAction) + LicenseKeyConstants.stDODELIM) + LicenseKeyConstants.stLICENSEKEYMODULES + LicenseKeyConstants.stCOLON + stModules) + LicenseKeyConstants.stDODELIM) + LicenseKeyConstants.stLICENSEKEYCREATIONDATE + LicenseKeyConstants.stCOLON + DateManager.ToStringWithInvariantCulture(creationDate)) + LicenseKeyConstants.stDODELIM;
            return string.Concat(new object[] { obj2, LicenseKeyConstants.stLICENSEKEYLICENSETYPE, LicenseKeyConstants.stCOLON, licenseType });
        }

        private void constructSecurityKeyAndIV(ref string stSecKey, ref string stIV)
        {
            stSecKey = "ILOVEMAYKID";
            stIV = "MAYKIDLOVESME";
        }

        private void createLicenseFile(string stLicenseKey, string stFileName, string stDirName)
        {
            FileManager.Write(stFileName, LicenseKeyConstants.stLICENSE_FILE_EXT, stLicenseKey);
        }

        private bool isDataValid(string stMachineName, int iNumUsers, DateTime creationDate, DateTime dtExpirationDate, string stExpirationAction, string stModules, string stFile, string stDir, ref string stError, string stBiosSerialNum)
        {
            if (StringManager.IsEmpty(stMachineName))
            {
                stError = MessageManager.MSG_LIC_002;
                return false;
            }
            if (iNumUsers <= 0)
            {
                return false;
            }
            if (dtExpirationDate.CompareTo(creationDate) <= 0)
            {
                stError = MessageManager.MSG_LIC_003;
                return false;
            }
            if (!Directory.Exists(stDir))
            {
                stError = MessageManager.MSG_LIC_004;
                return false;
            }
            stError = null;
            return true;
        }

        public string ProcessLicenseKey(string license, string file, string dir)
        {
            string stSecKey = null;
            string stIV = null;
            string outName = null;
            this.constructSecurityKeyAndIV(ref stSecKey, ref stIV);
            ASCIIEncoding encoding = new ASCIIEncoding();
            MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
            byte[] bytes = encoding.GetBytes(stSecKey);
            byte[] rijnKey = provider.ComputeHash(bytes);
            byte[] buffer = encoding.GetBytes(stIV);
            byte[] rijnIV = provider.ComputeHash(buffer);
            outName = dir + @"\" + file;
            if (!outName.Trim().EndsWith(LicenseKeyConstants.stLICENSE_FILE_EXT))
            {
                outName = outName + "." + LicenseKeyConstants.stLICENSE_FILE_EXT;
            }
            EncryptionManager.EncryptDataToFile(license, outName, rijnKey, rijnIV);
            return null;
        }

        public string ProcessLicenseKey(string stMachineName, int iNumUsers, DateTime dtExpirationDate, string stExpirationAction, string stModules, string stFile, string stDir, string stBiosSerialNum, LicenseKeyConstants.LicenseKeyType licenseType)
        {
            string license = null;
            string stError = null;
            string str3;
            try
            {
                if (!this.isDataValid(stMachineName, iNumUsers, DateTime.Now, dtExpirationDate, stExpirationAction, stModules, stFile, stDir, ref stError, stBiosSerialNum))
                {
                    return stError;
                }
                license = this.constructLicenseKey(stMachineName, iNumUsers, DateTime.Now, dtExpirationDate, stExpirationAction, stModules, stFile, stDir, stBiosSerialNum, licenseType);
                str3 = this.ProcessLicenseKey(license, stFile, stDir);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str3;
        }
    }
}

